﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Data;
using MyAlbum.Utility;

namespace MyAlbum.Services
{
    public class ReplyService : IDataService<Reply>
    {
        private IRepository<Reply> _repository;
        public ReplyService(IRepository<Reply> replyRepository)
        {
            if (replyRepository == null)
            {
                throw new ArgumentNullException("replyRepository");
            }
            this._repository = replyRepository;

        }
        #region IDataService<Reply> Members

        public List<Reply> GetItems()
        {
            return this._repository.GetItems();
        }

        public void AddItem(Reply reply, IValidationSummary validationSummary)
        {
            Assert.IsNotNull(reply);
            if (reply.EntryId == null)
            {
                validationSummary.AddError("EntryId", "Missing");
            }
            if (!FormsValidation.ContainsCharacters(reply.UserName))
            {
                validationSummary.AddError("UserName", "Missing");
            }
            if (!FormsValidation.ContainsCharacters(reply.Subject))
            {
                validationSummary.AddError("Subject", "Missing");
            }
           

            if (validationSummary.IsValid)
            {
                this._repository.AddItem(reply);
            }
        }

        public void DeleteItem(Reply item)
        {
            this._repository.DeleteItem(item);
        }

        public void UpdateItem(Reply reply, IValidationSummary validationSummary)
        {
            Assert.IsNotNull(reply);
            if (reply.EntryId == null)
            {
                validationSummary.AddError("EntryId", "Missing");
            }
            if (reply.UserName == null)
            {
                reply.UserName = string.Empty;
            }
            if (reply.Subject == null)
            {
                reply.Subject = string.Empty;
            }
            if (validationSummary.IsValid)
            {
                this._repository.UpdateItem(reply);
            }
        }


        public Reply GetItemById(string id)
        {
            return this._repository.GetItemById(id);
        }

        #endregion

    }
}
